export default {
    //将时间转为2020-12-12 格式
    getNowFormatDate(data) {
        var date = new Date(data);
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    },
    // 获取时间
    getTime(time, bool) {
        let date = new Date(time);
        let year = date.getFullYear();
        let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
        let day = date.getDate() > 9 ? date.getDate() : "0" + (date.getDate());
        if (bool) {
            day = "01";
        }
        return year + "-" + month + "-" + day;
    },
    //获取今天和上个月今天的时间
    getLastMonth() {
        var now = new Date();
        var year = now.getFullYear(); //getYear()+1900=getFullYear()
        var month = now.getMonth() + 1; //0-11表示1-12月
        var day = now.getDate();

        var dateObj = {};
        if (parseInt(month) < 10) {
            month = "0" + month;
        }
        if (parseInt(day) < 10) {
            day = "0" + day;
        }

        dateObj.now = year + '-' + month + '-' + day + ' 00:00:00';

        if (parseInt(month) == 1) { //如果是1月份，则取上一年的12月份

            dateObj.last = (parseInt(year) - 1) + '-12-' + day + ' 00:00:00';
            return dateObj;
        }

        var preSize = new Date(year, parseInt(month) - 1, 0).getDate(); //上月总天数
        if (preSize < parseInt(day)) { //上月总天数<本月日期，比如3月的30日，在2月中没有30
            dateObj.last = year + '-' + month + '-01' + ' 00:00:00';
            return dateObj;
        }

        if (parseInt(month) <= 10) {
            // 下面这行要删除
            // year = year - 2

            dateObj.last = year + '-0' + (parseInt(month) - 1) + '-' + day + ' 00:00:00';
            return dateObj;
        } else {

            dateObj.last = year + '-' + (parseInt(month) - 1) + '-' + day + ' 00:00:00';
            return dateObj;
        }

    },



    getDateRange(dateNow, intervalDays, bolPastTime) {
        let oneDayTime = 24 * 60 * 60 * 1000;
        let list = [];
        let lastDay;

        if (bolPastTime == true) {
            lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);
            list.push(this.formateDate(lastDay));
            list.push(this.formateDate(dateNow));
        } else {
            lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTime);
            list.push(this.formateDate(dateNow));
            list.push(this.formateDate(lastDay));
        }
        return list;
    },

    formateDate(time) {
        let year = time.getFullYear()
        let month = time.getMonth() + 1
        let day = time.getDate()

        month = month < 10 ? '0' + month : month;
        day = day < 10 ? '0' + day : day;

        return year + '-' + month + '-' + day
    }

}